package com.mairuide.excel_rule.controller;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.utils.JsonUtil;
import com.mairuide._frame.utils.ThreadPoolUtils;
import com.mairuide._frame.utils.mail.MailService;
import com.mairuide.bill.utils.ZipUtils;
import com.mairuide.entity.Result;
import com.mairuide.entity.StatusCode;
import com.mairuide.excel_rule.pojo.ExcelRule;
import com.mairuide.excel_rule.service.ExcelRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-11 14:54
 */
@Controller
@RequestMapping("rule")
public class ExcelRuleController extends BaseController {
    @Autowired
    ExcelRuleService excelRuleService;

    @Autowired
    MailService mailService;

    String path="D:\\MRD-7800\\template";

    @RequestMapping(method= RequestMethod.GET,value = "list")
    public Object findAll(){
        return "/rule/list";
    }
    @RequestMapping(method= RequestMethod.GET,value = "addPage")
    public Object addPage(){
        request.setAttribute("ruleId",0);
        return "/rule/addExcelRule";
    }
    @RequestMapping(method= RequestMethod.GET,value = "updatePage/{id}")
    public Object updatePage(@PathVariable(name = "id") Integer id){
        request.setAttribute("ruleId",id);
        return "/rule/addExcelRule";
    }
    @RequestMapping(method= RequestMethod.GET,value = "findExcelRuleById/{id}")
    @ResponseBody
    public Result findExcelRuleById(@PathVariable(name = "id") Integer id){
        ExcelRule excelRule1 = excelRuleService.findExcelRuleById(id);
        return new Result(true, StatusCode.OK,"操作成功",excelRule1);
    }
    /**
     * 新增/修改
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/addExcelRule",method= RequestMethod.POST)
    public Result addExcelRule(@RequestBody ExcelRule excelRule){
        ExcelRule excelRule1 = null;
        File[] srcfile = null;
        File file = null;
        try {
            if (excelRule.getIsUpAiYun()==1){
                String filePath=path+"\\"+excelRule.getTemplateName()+".zip" ;
                file = new File(path);
                File file1 = new File(path, "sql.txt");
                file1.createNewFile();

                //向新建的文本写数据
                FileWriter fw = new FileWriter(file1);
                fw.write(JsonUtil.obj2String(excelRule));
                fw.flush();
                fw.close();

                srcfile = file.listFiles();
                File zipfile = new File(filePath);
                ZipUtils.ZipFiles(srcfile,zipfile);
                ThreadPoolUtils.getInstance().getThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        mailService.sendAttachmentsMail("657083979@qq.com","Excel模板审核","有附件，请查收",filePath,path);
                    }
                });
            }
            excelRule1 = excelRuleService.addExcelRule(excelRule);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败",excelRule1);
        }
        return new Result(true, StatusCode.OK,"操作成功",excelRule1);
    }

    /**
     * 删除
     * @param id
     * @throws Exception
     */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    @ResponseBody
    public Result deleteExcelRule(@PathVariable Integer id) throws Exception{
        excelRuleService.delExcelRule(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/search")
    public Result findSearch( @RequestBody(required = false) ExcelRule excelRule){
        Page<ExcelRule> excelRulePage = excelRuleService.getExcelRuleList(excelRule);
        return new Result(true,StatusCode.OK,"查询成功",excelRulePage);
    }
    /**
     * 上传excel文件
     * @param searchMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/uploadFile")
    public Result uploadFile(@RequestParam(name = "file") MultipartFile file){
        try {
            File file1 = new File(path);
            //不存在就创建
            if(!file1.exists()){
                file1.mkdirs();
            }
            File file2 = new File(path, file.getOriginalFilename());
            file.transferTo(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(true,StatusCode.OK,"上传成功");
    }
    /**
     * 删除excel文件
     * @param searchMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/removeFile")
    public Result removeFile(@RequestParam(name = "fileName") String fileName){
        File file = new File(path,fileName);
        if(file.exists()){
            file.delete();
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
